<!-- 全局列表 table -->
<template>
    <div class="dy-table" v-loading="loading">
        <!-- 表头 -->
        <table-header :elTable_Options="props.elTable_Options" @bottonHandle="bottonHandle"
            @handleSelect="handleSelect" />
        <section class="table-content">
            <!-- 搜索 -->
            <dy-search :searchList="props.elTable_Options.searchList" ref="searchInstance"
                :allConfigList="props.elTable_Options.allConfigList" :userPreferences="props.elTable_Options.table_type"
                @conditionalQuery="conditionalQuery" @modifyPreference="modifyPreference" />
            <!-- 表格 -->
            <Table ref="table" :elTable_Options="props.elTable_Options" :maxHeight="props.maxHeight"
                @handleClick="handleClick" :searchParams="searchParams" :preferenceFiles="preferenceFiles" />
        </section>
        <!-- 导出弹框 -->
        <dy-export ref="myexport" v-if="modalStatus === '批量导出'" :eltableArr="props.elTable_Options.allConfigList"
            :allcount="allcount" :selectArr="selection" @exportDataList="exportDataList" />
        <!-- 导入弹框 -->
        <dy-import ref="myimport" v-if="modalStatus === '批量导入'" @gettabledata="getTableData" />
    </div>
</template>

<script setup>
import { ref, nextTick, toRefs, reactive, computed, onMounted  } from 'vue'
import { useCach } from '@/store/cach'
import commonV from '@/common/index'

let pageConfig = useCach()
//激活文本
let modalStatus = computed(() => pageConfig.modalStatus)

let searchParams = ref({})

let searchInstance = ref(null)

//喜好
let preferenceFiles = ref([])

const props = defineProps({
    elTable_Options: {
        type: Object,
        default: () => {
            return {}
        }
    },
    maxHeight: {
        type: Number,
        default: 460
    }
})

const emits = defineEmits(['handleClick', 'bottonHandle', 'handleSelect'])

//数据
let state = reactive({
    loading: false,
    allcount: 0,
    selection: []
})
let { loading, allcount, selection } = toRefs(state)

//点击事件
let handleClick = (row, index, orderNum, item) => {
    emits('handleClick', row, index, orderNum, item)
}

//search 搜索
let conditionalQuery = (params) => {
    searchParams.value = params
    //一定要加
    nextTick(() => {
        //获取数据
        conditional()
    })
}

//修改用户喜好
let modifyPreference = (files) => {
    preferenceFiles.value = files
}

//tab切换
let handleSelect = (key) => {
    emits('handleSelect', key)
}

//获取table信息
let table = ref(null)
let getTableData = (row) => {
    nextTick(() => {
        table.value && table.value.getTableData(row)
    })
}

//重置
let conditional = () => {
    table.value.conditionalQuery()
}

//给搜索参数赋值
let setSearchData = (searchVal) => {
    searchParams.value = searchVal
    searchInstance.value.setSearchData(searchVal)
}

let myexport = ref(null)
let myimport = ref(null)
//按钮点击事件
let bottonHandle = (way) => {
    if (way == 'exportData') {
        //导出
        state.allcount = table.value?.state.total
        state.selection = table.value?.state.selection
        pageConfig.changeShowModal(true)
        pageConfig.changeModalStatus('批量导出')
        nextTick(() => {
            myexport.value?.openFunction()
        })
        return
    }
    if (way == 'importData') {
        //导入
        pageConfig.changeShowModal(true)
        pageConfig.changeModalStatus('批量导入')
        let params = {
            templatename: props.elTable_Options.importexport.import_templatename,
            filename: props.elTable_Options.importexport.import_filename,
            uploadurl: props.elTable_Options.importexport.import_api
        }
        //如果需要传入用户id
        if (props.elTable_Options.importexport.user_id) {
            params.user_id = props.elTable_Options.importexport.user_id
        }
        nextTick(() => {
            myimport.value?.openFunction(1, params)
        })
    }
    emits('bottonHandle', way)
}

//导出回调
let exportDataList = async (keys, selection) => {
    let params = {}
    //全导出
    if (!selection.length) {
        //搜索栏参数
        let search = commonV1.formatSearchParams(props.elTable_Options.searchList, searchParams.value)
        params = search
        //额外table参数
        if (props.elTable_Options.addConditions) {
            props.elTable_Options.addConditions.forEach(it => {
                params[it.key] = it.value
            })
        }
        //额外导出参数
        if (props.elTable_Options.importexport?.addConditionparam) {
            let addConditionparam = props.elTable_Options.importexport.addConditionparam
            for (let key in addConditionparam) {
                params[key] = addConditionparam[key]
            }
        }
    }

    //导出部分
    let paramsObj = {}
    keys.forEach(item => {
        props.elTable_Options.allConfigList.forEach(it => {
            if (item === it.label) {
                paramsObj[item] = it.prop
            }
        })
    })
    params.field_dic = paramsObj
    state.loading = true
    await commonV.exportDataList(
        selection,
        props.elTable_Options.importexport.export_filename,
        props.elTable_Options.importexport.export_api,
        params
    )
    state.loading = false
}

//列表选中项
let getTableSelection = () => {
    return table.value.getSelection() || []
}

//导出方法
defineExpose({ getTableData, conditional, tableIntance: table, setSearchData, searchParams, getTableSelection })
</script>

<style lang="scss" scoped>
.dy-table {
    width: 100%;
    padding: 20px;
    background-color: #fff;
}
</style>